﻿using IOA.MES.WebTool.Common;
using Seagull.BarTender.Print;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;

namespace IOA.MES.WebTool
{
    /// <summary>
    /// WinSetting.xaml 的交互逻辑
    /// </summary>
    public partial class WinSetting : System.Windows.Window
    {
        public WinSetting()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var curApiRoot = BaseEnv.GetStorage("ApiRoot");
            var ftp = BaseEnv.GetStorage("Ftp");
            var ftpUserName = BaseEnv.GetStorage("FtpUserName");
            var ftpPassword = BaseEnv.GetStorage("FtpPassword");
            var imageFolder = BaseEnv.GetStorage("ImageFolder");
            if (!string.IsNullOrWhiteSpace(curApiRoot))
            {
                txtApiRoot.Text = curApiRoot;
            }
            if (!string.IsNullOrWhiteSpace(ftp))
            {
                txtFtp.Text = ftp;
            }
            if (!string.IsNullOrWhiteSpace(ftpUserName))
            {
                txtUserName.Text = ftpUserName;
            }
            if (!string.IsNullOrWhiteSpace(ftpPassword))
            {
                txtPassword.Text = ftpPassword;
            }
            if (!string.IsNullOrWhiteSpace(imageFolder))
            {
                txtImageFolder.Text = imageFolder;
            }

            var printers = new Printers();
            var dataSource = new List<string>();
            foreach (Printer printer in printers)
            {
                dataSource.Add(printer.PrinterName);
            }
            cbMarkPrinters.ItemsSource = dataSource;
            cbLabelPrinters.ItemsSource = dataSource;

            var curMarkPrinter = BaseEnv.GetStorage("Printer");
            if (!string.IsNullOrWhiteSpace(curMarkPrinter) && dataSource.Contains(curMarkPrinter, StringComparer.CurrentCultureIgnoreCase))
            {
                cbMarkPrinters.SelectedValue = curMarkPrinter;
            }

            var curLabelPrinter = BaseEnv.GetStorage("LabelPrinter");
            if (!string.IsNullOrWhiteSpace(curLabelPrinter) && dataSource.Contains(curLabelPrinter, StringComparer.CurrentCultureIgnoreCase))
            {
                cbLabelPrinters.SelectedValue = curLabelPrinter;
            }
        }

        private void btnCheck_Click(object sender, RoutedEventArgs e)
        {
            var resposne = BaseEnv.InvokeApi<object>("/api/Auth/Ping", "", new { }, txtApiRoot.Text.Trim());
            if (resposne.ErrCode != 200)
            {
                MessageBox.Show(resposne.ErrMsg);
            }
            else
            {
                MessageBox.Show("校验成功！");
            }
        }

        private void btnSelect_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new System.Windows.Forms.FolderBrowserDialog();
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK && Directory.Exists(dialog.SelectedPath))
            {
                txtImageFolder.Text = dialog.SelectedPath;
            }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            var apiRoot = txtApiRoot.Text.Trim();
            var markPrinter = Convert.ToString(cbMarkPrinters.SelectedValue) ?? string.Empty;
            var labelPrinter = Convert.ToString(cbLabelPrinters.SelectedValue) ?? string.Empty;
            var ftp = txtFtp.Text.Trim();
            var userName = txtUserName.Text.Trim();
            var password = txtPassword.Text.Trim();
            var imageFolder = txtImageFolder.Text.Trim();
            if (string.IsNullOrWhiteSpace(apiRoot))
            {
                MessageBox.Show("接口地址是必填项");
                return;
            }

            BaseEnv.ApiRoot = apiRoot;
            BaseEnv.MarkPrinter = markPrinter;
            BaseEnv.LabelPrinter = labelPrinter;
            BaseEnv.Ftp = ftp;
            BaseEnv.FtpUserName = userName;
            BaseEnv.FtpPassword = password;
            BaseEnv.ImageFolder = imageFolder;
            BaseEnv.SetStorage("ApiRoot", apiRoot);
            BaseEnv.SetStorage("Printer", markPrinter);
            BaseEnv.SetStorage("LabelPrinter", labelPrinter);
            BaseEnv.SetStorage("Ftp", ftp);
            BaseEnv.SetStorage("FtpUserName", userName);
            BaseEnv.SetStorage("FtpPassword", password);
            BaseEnv.SetStorage("ImageFolder", imageFolder);
            this.Close();
        }
    }
}
